<?php

namespace App\Http\Controllers\Home;

use Illuminate\Http\Request;

use App\Http\Requests;
use DB;
use Hash;
use Cookie;
use Session;
use App\Http\Controllers\Common\YunpianController;
use App\Http\Controllers\Controller;

class RegController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    //登入页面
    public function getIndex()
    {
        return view('home.reg.index');
    }


    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    //注册
    public function postCreate(Request $request)
    {

        $info =$request->input();
        $rec = Db::table('member')->where('phone',$info['phone'])->first();
        $value = $request->session()->get('yzm');
        if($rec){
            $status = 1;
            $msg = '该用户已存在';
        }else{
            if($info['yzm'] == $value){
                $data = array(
                    'phone'=>$info['phone'],
                    'pwd' =>Hash::make($info['pwd']),
                );
                Db::table('member')->insert($data);
                $status = 2;
                $msg = '注册成功';
            }else{
                $status=3;

                $msg = '手机验证码错误';
            }

        }
        return ['status'=>$status,'msg'=>$msg];
    }

    // 短信发送验证码
    public function postCode(Request $request)
    {
        $phone = $request->input('mobile');
//        dd($phone);die;
        $codes = '0123456789';
        $num = '';
        for ($i=0;$i<4;$i++)
        {
            $num = $num.substr(str_shuffle($codes),0,1);
        }
        if ($phone && $num) {
            $text = "【三七物联管理平台】您的验证码是".$num;
            $yunpian = new YunpianController();
            $msg = $yunpian->Msgsend($phone, $text);
            $request->session()->put('yzm', $num);
            return $num;
        }else{
            return '';
        }
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        //
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        //
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        //
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
        //
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        //
    }
}
